import * as types from "../types/recycleAccessResultTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";

export function initOrderRecycle(facility_id, recycleValues) {
  return dispatch => {
    dispatch({ type: types.RECYCLEACCESSRESULT_INIT });
    const recycleLabelValues = [];
    for (let x = 0; x < recycleValues.length; x += 1) {
      const valueX = recycleValues[x];
      for (let y = 0; y < valueX.recycleInfo.length; y += 1) {
        recycleLabelValues.push({
          recycle_lable_id: valueX.recycle_lable_id,
          recycle_price_id: valueX.recycleInfo[y].recycle_price_id,
        });
      }
    }
    NetWorkUtil.post(ApiConstants.RECYCLE_SELECTPROJECT, {
      facility_id,
      recycle_lable_data: JSON.stringify(recycleLabelValues),
    })
      .then(res => {
        const { facility_recycle } = res;
        NetWorkUtil.post(ApiConstants.RECYCLE_SELECTPORDERSHOW, { facility_recycle, facility_id })
          .then(data => {
            dispatch({
              type: types.RECYCLEACCESSRESULT_DONE,
              data: data.list,
              recycleTypeList: data.recycle_type_list,
              facilitySign: facility_recycle,
            });
          })
          .catch(error => {
            dispatch({ type: types.RECYCLEACCESSRESULT_INIT_ERROR, error });
          });
      })
      .catch(error => {
        dispatch({ type: types.RECYCLEACCESSRESULT_ERROR, error });
      });
  };
}

export function getDefaultAddress() {
  return dispatch => {
    NetWorkUtil.post(ApiConstants.GET_DEFAULTADDRESS, {})
      .then(address => {
        dispatch({ type: types.RECYCLEACCESSRESULT_CHANGE_ADDRESS, address });
      })
      .catch(error => {});
  };
}

export function planRecycleOrder(
  facility_recycle,
  facility_id,
  recycle_type,
  recycle_time,
  transaction_type,
  line_id,
  station_id,
  address,
  callback,
) {
  return dispatch => {
    if (transaction_type === 1 && !address) {
      toastShort("请先设置默认地址");
      return;
    }

    const params = {
      facility_recycle,
      facility_id,
      recycle_type,
      recycle_time,
      transaction_type,
      line_id,
      station_id,
      delivery_address_id: address.delivery_address_id,
    };
    NetWorkUtil.post(ApiConstants.RECYCLE_PLACEORDERSHOW, params)
      .then(res => {
        dispatch({ type: types.RECYCLEACCESSRESULT_PLANORDER_DONE });
        callback(res);
      })
      .catch(error => {});
  };
}

export function changeRecycleWay(newIndex) {
  return dispatch => {
    dispatch({ type: types.RECYCLEACCESSRESULT_CHANGE_RECYCLEWAY, curRecycleWayIndex: newIndex });
  };
}

export function changeDefaultAddress(address) {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_CHANGE_DEFAULTADDRESS, address });
  };
}

export function changeRealType(realType) {
  return dispatch => {
    dispatch({ type: types.RECYCLEACCESSRESULT_CHANGE_REALWAY, realType });
  };
}

export function changeStationName(pickerValue) {
  return dispatch => {
    dispatch({
      type: types.RECYCLEACCESSRESULT_CHANGE_SUBWAYNAME,
      stationName: { station_name: pickerValue[1], line_name: pickerValue[0] },
    });
  };
}

export function changeStationCode(pickerValue) {
  return dispatch => {
    dispatch({ type: types.RECYCLEACCESSRESULT_LOADING });
    NetWorkUtil.post(ApiConstants.STATION_LINE, {})
      .then(lineList => {
        const lineName = pickerValue[0];
        const lineCode = lineList.find(item => item.name === lineName).id;
        NetWorkUtil.post(ApiConstants.STATION_STATION, { id: lineCode })
          .then(stationList => {
            const stationCode = stationList.find(item => item.name === pickerValue[1]).id;
            dispatch({
              type: types.RECYCLEACCESSRESULT_CHANGE_SUBWAYCODE,
              stationCode: {
                line_id: lineCode,
                station_id: stationCode,
              },
            });
          })
          .catch(error => dispatch({ type: types.RECYCLEACCESSRESULT_ERROR, error }));
      })
      .catch(error => dispatch({ type: types.RECYCLEACCESSRESULT_ERROR, error }));
  };
}

export function changeDatetime(dateTime) {
  return dispatch => {
    dispatch({ type: types.RECYCLEACCESSRESULT_CHANGE_DATETIME, dateTime });
  };
}

export function unmount() {
  return dispatch => {
    dispatch({ type: types.RECYCLEACCESSRESULT_UNMOUNT });
  };
}

export function getAllStationJson() {
  const data = [];
  return dispatch => {
    NetWorkUtil.getWithoutLogin("Data/getSubwayLine", {})
      .then(linList => {
        for (let x = 0; x < linList.length; x += 1) {
          const { name, id } = linList[x];
          data.push({ name, station: [] });
          NetWorkUtil.getWithoutLogin("Data/getSubwaystation", { id })
            .then(stationList => {
              for (let y = 0; y < stationList.length; y += 1) {
                const stationName = stationList[y].name;
                data[x].station.push(stationName);
              }
              console.log(JSON.stringify(data));
            })
            .catch(error => {});
        }
      })
      .catch(error => {});
  };
}
